package com.friday.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.friday.bean.Employee;
import com.friday.dao.EmployeeDao;
import com.friday.util.UploadImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;


    @RequestMapping("/login")
    public JSONObject checkLogin(@RequestParam("id") int id,@RequestParam("password") String password){
        JSONObject result=new JSONObject();
        //返回是否查到
        int ifExist=employeeDao.checkExist(id,password);
        if(ifExist>0)
        {
            //若查到，则返回成功信息
            JSONObject employee=employeeDao.getEmployeeById(id);
            result.put("result","success");
            result.put("user",employee);
        }else{
            //若未查到，返回失败信息
            result.put("result","fail");
            result.put("user",null);
        }
        return result;
    }

    @RequestMapping("/employeeList")
    public JSONArray getAllEmployee(){
        return employeeDao.getAllEmployee();
    }

    @RequestMapping("/employeeDelete")
    public String delEmployee(@RequestParam("id") int id){
        int tag = employeeDao.delEmployeeById(id);
        if( tag > 0){
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/employeeAdd")
    public JSONObject addEmployee(@RequestBody JSONObject employee){
        JSONObject result =new JSONObject();
        int tag = employeeDao.addEmployee(employee);
        if( tag>0 ){
            result.put("result","success");
            result.put("employee_id",employee.getInteger("id"));
        }else {
            result.put("result","fail");
            result.put("employee",null);
        }
        return result;
    }

    @RequestMapping("/employeeEdit")
    public JSONObject editEmployee(@RequestBody JSONObject employee){
        int tag = employeeDao.editEmployee(employee);
        JSONObject result=new JSONObject();
        if(tag>0)
        {
            JSONObject user=employeeDao.getEmployeeById(employee.getInteger("id"));
            result.put("result","success");
            result.put("user",user);
        }else
        {
            result.put("result","fail");
            result.put("user",null);
        }
        return result;
    }

    @RequestMapping("/employeeAvatarEdit")
    public JSONObject editEmployeeSelf(int id, MultipartFile file){
        //删除原来的图片
        JSONObject employee=employeeDao.getEmployeeById(id);
        UploadImg.delete(employee.getString("avatar"));

        //放入新的图片
        String url=UploadImg.upload(file);
        JSONObject avatar=new JSONObject();
        avatar.put("id",id);
        avatar.put("avatar",url);
        JSONObject result=new JSONObject();
        int tag = employeeDao.editEmployee(avatar);
        if(tag>0)
        {
            employee=employeeDao.getEmployeeById(id);
            result.put("result","success");
            result.put("user",employee);
        }else
        {
            result.put("result","fail");
            result.put("user",null);
        }
        return result;
    }

    @RequestMapping("/emPwdEdit")
    public JSONObject pwdEdit(int id,String password){
        int tag=employeeDao.changePassword(id,password);
        JSONObject result=new JSONObject();
        if(tag>0)
        {
            JSONObject employee=employeeDao.getEmployeeById(id);
            result.put("result","success");
            result.put("user",employee);
        }else
        {
            result.put("result","fail");
            result.put("user",null);
        }
        return result;
    }


    @RequestMapping("/orderEmployeeList")
    public JSONArray getOrderEmployee(){
        return employeeDao.getOrderEmployee();
    }













}
